package com.linkoog.devtools.dubbo.toolwindow.workbenche.left.list;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBList;
import com.intellij.util.ui.JBFont;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.left.DubboHistory;
import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;

public class DubboHistoryJList extends JBList<DubboHistory> {

    public DubboHistoryJList(@NotNull ListModel<DubboHistory> dataModel) {
        super(dataModel);
        setEmptyText("No dubbo history available");
        setCellRenderer(new DubboHistoryRender());
    }


    private static class DubboHistoryRender extends JPanel implements ListCellRenderer<DubboHistory>{
        private final static Icon successIcon = IconLoader.getIcon("/icons/dubbo/status/success.svg", DubboHistoryRender.class);
        private final static Icon failIcon = IconLoader.getIcon("/icons/dubbo/status/fail.svg", DubboHistoryRender.class);

        private final JLabel statusIcon = new JLabel();
        private final JBLabel buildNumber = new JBLabel();
        private final JBLabel nameLabel = new JBLabel();
        private final JBLabel timeLabel = new JBLabel();

        private final JBLabel messageLabel = new JBLabel();


        private JBColor selectionBackground = new JBColor(0xE9EEF5, 0x464A4D);
        private JBColor unfocusedSelectionBackground = new JBColor(0xF5F5F5, 0x464A4D);

        public DubboHistoryRender() {
            setBorder(JBUI.Borders.empty(5, 6));
            setLayout(new MigLayout(new LC().gridGap("10", "0").insets("0", "0", "0", "0").flowX()));

            String gapAfter = JBUI.scale(5) +"px";

            statusIcon.setVerticalAlignment(SwingConstants.CENTER);
            add(statusIcon, new CC().spanY(3).gapAfter(JBUI.scale(2) + "px"));


            final JBFont boldFont = JBFont.label().biggerOn(2).asBold();
            buildNumber.setFont(boldFont);
            buildNumber.setHorizontalTextPosition(SwingConstants.LEFT);
            nameLabel.setFont(boldFont);
            nameLabel.setHorizontalTextPosition(SwingConstants.LEFT);

            timeLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
            add(buildNumber,"align left, ");
            add(nameLabel, "align left, push");
            add(timeLabel, "align right, wrap");


            add(messageLabel,new CC().spanX(3).wrap());
        }



        @Override
        public Component getListCellRendererComponent(JList<? extends DubboHistory> jList, DubboHistory dubboHistory, int index, boolean isSelected, boolean cellHasFocus) {
            //setPreferredSize(new Dimension(jList.getVisibleRect().width, 50));
            //setSize(new Dimension(jList.getVisibleRect().width, 50));


            UIUtil.setBackgroundRecursively(this, background(jList, isSelected, jList.hasFocus()));
            Color primaryTextColor = foreground(isSelected, jList.hasFocus());
            Color secondaryTextColor = secondaryForeground(jList, isSelected);

            statusIcon.setIcon(dubboHistory.isSuccess()? successIcon: failIcon);

            buildNumber.setText("#" + dubboHistory.getSerialNum());
            buildNumber.setForeground(primaryTextColor);

            nameLabel.setText(dubboHistory.getMethodName());
            nameLabel.setForeground(primaryTextColor);

            if (dubboHistory.getStartTime() != null){
                timeLabel.setText(new SimpleDateFormat("HH:mm:ss").format(dubboHistory.getStartTime()));
            }

            messageLabel.setText(dubboHistory.getInface() + "#" + dubboHistory.getMethodName());
            messageLabel.setForeground(secondaryTextColor);
            return this;
        }


        private Color background(JList<?> list, boolean isSelected, boolean hasFocus) {
            if (isSelected){
                return hasFocus?
                        JBColor.namedColor("Table.lightSelectionBackground", selectionBackground)
                        : JBColor.namedColor("Table.lightSelectionInactiveBackground", unfocusedSelectionBackground);
            }
            return  list.getBackground();
        }

        private Color foreground(boolean isSelected, boolean hasFocus){
            Color defaultColor = UIUtil.getListForeground();
            if (isSelected) {
                return hasFocus? JBColor.namedColor("Table.lightSelectionForeground", defaultColor)
                        : JBColor.namedColor("Table.lightSelectionInactiveForeground", defaultColor);
            } else{
                return JBColor.namedColor("Table.foreground", defaultColor);
            }
        }

        private Color secondaryForeground(JList<?> list,boolean isSelected){
            if (isSelected) {
                return foreground(true, list.hasFocus());
            } else {
                return JBColor.namedColor("Component.infoForeground", UIUtil.getContextHelpForeground());
            }
        }
    }
}
